/**
 * Created by python on 19-4-13.
 */
var app = new Vue({
    el: "#app",
    data: {
        host: host,
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        avatar_url: sessionStorage.avatar_url || localStorage.avatar_url,
        token: sessionStorage.token || localStorage.token,
        nick_name: sessionStorage.nick_name || localStorage.nick_name,
        // 页面中需要使用到的数据，键值对
        newsdetail: null,
        id: null,
        commentparent: 0,
        commentlist: [],
        commentlists: [],
        comments: [],      // 父评论循环列表
        comments1: [],     // 子评论循环列表
        commentinput: '',   // 评论输入
        listtocao: null,
        listques: null,
        person:null,

    },
    mounted: function () {

        this.newsinfo();
        this.tucaoinfo();
        this.quesinfo();
        this.personinfo();

    },
    methods: {
        // hui:function (id,len) {
        //     alert(id)
        //     alert(len)
        //     this.is = 0
        //     for(var i=0;i<len;i++){
        //         if (id=i){
        //             this.is=1
        //         }
        //     }
        //
        // },

        // 添加评论
        create_comment: function () {

            // console.log(this.commentlist),
            // console.log(this.id),

            axios.post(host + "/createcomment/", {
                content: this.commentinput,
                news_id: this.id

            }, {
                // 向后端传递JWT token的方法
                headers: {
                    // 后台接收的JWT对格式有要求,'JWT'开头,空一格拼接字符串
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json',
            })
                .then(response => {
                    // console.log(response.data);
                    this.commentlists = response.data;
                    this.commentlist = this.get_commentparent_null_count("this.commentlists")
                    location.reload();
                    // console.log(this.commentlist)
                })
                .catch(error => {
                    alert("请登录后评论！")
                })
        },


        // 新闻加评论
        newsinfo: function () {
            // 一加载就需要做的，直接是代码

            this.id = this.get_query_string("id", 1000)

            axios.get(host + "/news/" + this.id)
                .then(response => {

                    this.newsdetail = response.data;
                    this.commentlists = this.newsdetail.news_comments;
                    this.commentlist = this.get_commentparent_null_count(this.commentlists)

                })
                .catch(error => {
                })
        },

        tucaoinfo: function () {
            axios.get(host + "/listtucao/")
                .then(response => {

                    this.listtocao = response.data
                    // console.log(this.listtocao)

                })
                .catch(error => {
                })
        },

        quesinfo: function () {
            axios.get(host + "/newsques/")
                .then(response => {

                    this.listques = response.data
                    // console.log(this.listques)
                }).catch(error => {
            })
        },


        personinfo: function () {
            axios.get(host + "/newsperson/")
                .then(response => {
                    this.person = response.data
                }).catch(error => {
            })
        },

        logout: function () {
            sessionStorage.clear();
            localStorage.clear();
            location.href = './person-loginsign.html';
        },
        // 需要用到的函数，键值对 ，键是名称，值是匿名函数

        // 获取url路径参数
        get_query_string: function (name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        },
        // 将评论按照父子分装列表
        get_commentparent_null_count: function (data) {
            //遍历该文章所有评论
            // console.log(data[1].parent)
            for (var i = 0; i < data.length; i++) {
                if (data[i].parent == null) {
                    // 父评论个数
                    this.commentparent += 1
                    // 父评论列表
                    this.comments.push(data[i])
                    // console.log(this.comments)
                }
                else {
                    // 子评论列表
                    this.comments1.push(data[i])
                }
            }
            //
            // console.log(this.comments1)
            // console.log(typeof this.commentparent)

            return [this.comments, this.comments1]
        },

    },
});



// $(".hover").hover(function () {
//         $(".hoverinfo").stop().animate({
//             opacity: "show"
//         }, "slow");
//         $(this).find(".fa-plus").css("color", "#e64620");
//     }, function () {
//         $(".hoverinfo").stop().animate({
//             opacity: "hide"
//         }, "slow");
//         $(this).find(".fa-plus").css("color", "#000");
//     })
//
// $(".callback").click(function () {
//         $(this).parents(".text").siblings(".edit-box").toggle();
//     });
//
// var onOff = true;
// $(".btn-attention").click(function () {
//         if ($(this).onOff) {
//             $(this).text("关注");
//             $(this).css({
//                 "color": "#e64620",
//                 "cursor": "not-allowed",
//                 "background": "#f5f5f5",
//                 "border-color": "#f5f5f5f5"
//             })
//             $(this).onOff = false;
//         } else {
//             $(this).text("已关注");
//             $(this).css("color", "#999");
//             $(this).onOff = true;
//         }
//     })
